#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=10000+5;
int n,len,a[10];
int m,pp;
int dia,fh,p,las;
int num,whe;
string word;
char st[N];
struct Node
{
	int num[10];
	int bh;
}net[N];
int main()
{
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	cin>>n;
	for (int i=1;i<=n;i++)
	{
		scanf("%s",st+1);
		len=strlen(st+1);
		word=""; 
		for (int j=1;j<=len;j++)word=word+st[j];
		
		scanf("%s",st+1);
		len=strlen(st+1);
        dia=0;fh=0;p=0;
        for (int j=1;j<=len;j++)
          if (st[j]=='.') dia++;
            else if (st[j]==':') fh++;
        if (dia!=3||fh!=1) p=1;
        dia=0;fh=0;
        for (int j=1;j<=len;j++)
          if (st[j]=='.'&&fh==0) dia++;
            else if (st[j]==':') fh++;
        if (dia!=3||fh!=1) p=1;
        if (p==0)
        {
        	for (int j=1;j<len;j++)
        	  if (st[j]=='.'||st[j]==':')
        	    if (st[j+1]=='.'||st[j+1]==':') p=1;
        	for (int j=1;j<=len-2;j++)
	          if ((st[j]=='.'||st[j]==':')&&st[j+1]=='0'&&('0'<=st[j+2]&&st[j+2]<='9')) 
	          { 
	              p=2;break;
			  }
		}
		if (p!=0) {printf("ERR\n");continue;}
		num=0;whe=0;las=0;
		for (int j=1;j<=len;j++)
		  if (st[j]=='.'||st[j]==':') 
		  {
		  	if (las!=j-1){whe++;a[whe]=num;num=0;}
		  	las=j;
		  }
		    else 
			{
				num=num*10+(st[j]-'0');
				if (num>100000) 
				{
					p=1;
					break;
				}
			}
		if (las!=len) {whe++;a[whe]=num;num=0;las=len;}
		if (whe!=5) {printf("ERR\n");continue;}
		
		for (int j=1;j<=4;j++) if (a[j]>255) p=1;
		if (a[5]>65535) p=1;
		if (p!=0) {printf("ERR\n");continue;}
		
		if (word=="Server")
		{
			p=0;
			for (int j=1;j<=m;j++)
			{
				pp=1;
				for (int k=1;k<=5;k++)
				  if (net[j].num[k]!=a[k])
				  {
				  	pp=0;
				  	break;
				  }
				if (pp==1) 
				{
					p=1;break;
				}
			}
			if (p==1) {printf("FAIL\n");continue;}
			m++;
			for (int k=1;k<=5;k++)  net[m].num[k]=a[k];
			net[m].bh=i;
			printf("OK\n");
			  
		}else
		if (word=="Client")
		{
			p=0;
			for (int j=1;j<=m;j++)
			{
				pp=1;
				for (int k=1;k<=5;k++)
				  if (net[j].num[k]!=a[k])
				  {
				  	pp=0;
				  	break;
				  }
				if (pp==1) 
				{
					p=j;break;
				}
			}
			if (p==0) {printf("FAIL\n");continue;}
			printf("%d\n",net[p].bh);
		}
	}
	return 0;
}
